﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using LayerDTO;

namespace LayerDAO
{
    public class CT_PhieuNhapDAO
    {
        public List<CT_PhieuNhapDTO> LayDanhSachCTPhieuNhap(string connect, string a_Ma)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("LayDanhSachCTPNTheoPN", new SqlParameter("@maPN", a_Ma), par);
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            List<CT_PhieuNhapDTO> list = new List<CT_PhieuNhapDTO>();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CT_PhieuNhapDTO ctpn = new CT_PhieuNhapDTO();
                    ctpn.STT = i + 1;
                    ctpn.MaCTPN = dt.Rows[i]["MCTPN"].ToString();
                    ctpn.MaPN = dt.Rows[i]["MPN"].ToString();
                    ctpn.MaMatHang = dt.Rows[i]["MMH"].ToString();
                    ctpn.SoLuongNhap = int.Parse(dt.Rows[i]["SoLuongNhap"].ToString());
                    ctpn.DonGiaNhap = float.Parse(dt.Rows[i]["DonGiaNhap"].ToString());
                    ctpn.MaDV = dt.Rows[i]["MDV"].ToString();
                    ctpn.ThanhTien = float.Parse(dt.Rows[i]["ThanhTien"].ToString());

                    ctpn.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());
                    list.Add(ctpn);
                }
            }
            return list;
        }
        public string ThemCTPN(CT_PhieuNhapDTO ctpn, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("ThemCTPNTheoPN", new SqlParameter("@maPN", ctpn.MaPN),
                                                             new SqlParameter("@maMH", ctpn.MaMatHang),
                                                             new SqlParameter("@soLuong", ctpn.SoLuongNhap),
                                                             new SqlParameter("@donGiaNhap", ctpn.DonGiaNhap),
                                                             new SqlParameter("@maDV", ctpn.MaDV), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }

        public string SuaCTPN(CT_PhieuNhapDTO ctpn, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("SuaCTPN", new SqlParameter("@maCTPN", ctpn.MaCTPN),
                                                            new SqlParameter("@maPN", ctpn.MaPN),
                                                             new SqlParameter("@maMH", ctpn.MaMatHang),
                                                             new SqlParameter("@soLuong", ctpn.SoLuongNhap),
                                                             new SqlParameter("@donGiaNhap", ctpn.DonGiaNhap),
                                                             new SqlParameter("@maDV", ctpn.MaDV),
                                                              new SqlParameter("@daXoa", ctpn.DaXoa), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
        public string XoaCTPN(CT_PhieuNhapDTO ctpn, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("XoaCTPN", new SqlParameter("@maCTPN", ctpn.MaCTPN), par);


            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
    }
}